var server = require('../../utils/server');
var common = require('../../utils/common');
var hideTimer = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    singleOrderList: '',
    reasonList: [{ "id": 1, name: "临时有事，不要了" }, { "id": 2, name: "配送速度太慢" }, { "id": 3, name: "商品与预期不符/商品有错误" }, { "id": 6, name: "优惠券无法使用" }, { "id": 4, name: "骑手提前点送达" }, { "id": 5, name: "其他" }],
    showReason: false,
    curReason: "",
    reasonId: "",
    memo: "",
    orderStatus:"",
    showDelPop:false,
    isCancel:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    })
    this.getOrderDetail();
  },
  onShow: function () {

  },
  onUnload(){
    clearTimeout(hideTimer);
  },
  onHide() {
    clearTimeout(hideTimer);
  },
  hideToast() {
    hideTimer = setTimeout(function () {
      wx.hideToast()
    }, 3500)
  },
  getOrderDetail() {
    common.getOrdeDetail(this.data.orderId).then(res => {
      this.setData({
        singleOrderList: res.data,
        orderStatus:res.data.status.code
      })
    })
  },
  bottomAnimate() {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(400).step()
    this.setData({
      animationData: animation.export(),
      showReason: !this.data.showReason
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  cancelOrder() {
    if (this.data.reasonId!=""){
      this.setData({
        showDelPop:true
      })
    }else{
      this.bottomAnimate();
      common.showToast("请选择取消原因");
      this.hideToast();
    }
  },
  confirmDel(){
    this.setData({
      isCancel:true
    })
    server.postJSON(common.storePath + 'order/cancel/', {
      "order_id": this.data.orderId,
      "cancel_reason_id": this.data.reasonId,
      "cancel_memo": this.data.memo
    }, res => {
      if (res.data.errno == "0") {
        this.setData({
          showDelPop: false,
          isCancel: false
        })
        wx.redirectTo({
          url: 'orderDetail?orderId=' + this.data.orderId
        })
      } else {
        this.setData({
          isCancel: false
        })
        common.showErrorToast(res.data.errmsg)
      }
    })
  },
  cancelDel(){
    this.setData({
      showDelPop: false
    })
  },
  selectReason(e) {
    this.data.reasonId = e.currentTarget.dataset.id;
    this.setData({
      curReason: e.currentTarget.dataset.reason
    })
    this.bottomAnimate();
  },
  inputMemo(e) {
    this.setData({
      memo: e.detail.value
    })
  }
})